package net.dotool;

import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

/**
 * @author 李岩飞
 * @email eliyanfei@126.com 2018年1月15日 上午10:50:24
 * 
 */
public class RequestTools {
	/**
	 * 获取request的指定参数，不填获取所有参数
	 * 
	 * @param request
	 * @param names
	 * @return
	 */
	public static <T> Map<String, T> getParameters(HttpServletRequest request, String[] names, Class<T> cla) {
		Map<String, T> params = new HashMap<String, T>();
		if (names == null) {
			Enumeration<String> nameArr = request.getParameterNames();
			while (nameArr.hasMoreElements()) {
				String name = nameArr.nextElement();
				params.put(name, cla.cast(request.getParameter(name)));
			}
		} else {
			for (String name : names) {
				params.put(name, cla.cast(request.getParameter(name)));
			}
		}
		return params;
	}

	public static Map<String, String> getParameters(HttpServletRequest request) {
		Map<String, String> params = new HashMap<String, String>();
		Enumeration<String> nameArr = request.getParameterNames();
		while (nameArr.hasMoreElements()) {
			String name = nameArr.nextElement();
			params.put(name, request.getParameter(name));
		}
		return params;
	}

	public static Map<String, ? extends Object> getParameters(HttpServletRequest request, String[] names) {
		return getParameters(request, names, String.class);
	}
}
